/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 */
package spwodne;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.table.AbstractTableModel;

public class MTSpolek extends AbstractTableModel {

    MTSpolek() {
        l = new ArrayList();
    }

    public List getSpL() {
        return l;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return l.size();
    }

    public static List getLSpolek() {
        return l;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col) {
        Object tmp;
        switch (col) {
            case 0: // '\0'

                tmp = ((Spolka) l.get(row)).id;
                break;

            case 1: // '\001'

                tmp = ((Spolka) l.get(row)).getNaz();
                break;

            case 2: // '\002'

                tmp = ((Spolka) l.get(row)).getStawka();
                break;

            default:
                System.out.println("getValue out of bounds");
                tmp = null;
                break;
        }
        return tmp;
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public void addRow(String nazwa, float stawka) {
        l.add(new Spolka(Integer.valueOf(l.size() + 1), nazwa, new Float(stawka)));
        fireTableDataChanged();
    }

    public void editRow(int nr, String nazwa, float stawka) {
        ((Spolka) l.get(nr)).setNaz(nazwa);
        ((Spolka) l.get(nr)).setSt(stawka);
        fireTableDataChanged();
    }

    public void delRow(int n) {
        l.remove(n);
        fireTableDataChanged();
    }

    public int zapiszPilk(File nazwa) {
        try {
            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(nazwa)));
            out.writeObject(new Date());
            out.writeObject(l);
            out.close();
            return 0;
        } catch (IOException e) {
            System.out.print("IOexception");
            e.printStackTrace();
            System.out.println(e.getMessage());
            return -1;
        }
    }

    public int czytajPlik(File nazwa) {
        try {
            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(nazwa)));
            d = (Date) in.readObject();
            l = (ArrayList) in.readObject();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IOexception");
            return -1;
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            System.out.println("Class not found");
            return -2;
        } catch (RuntimeException e) {
            System.out.println("Runtime exception");
            return -3;
        }
        fireTableDataChanged();
        return 0;
    }

    public Date getDate() {
        return d;
    }

    public void addNal(int nr) {
        float ile = ((Spolka) l.get(nr)).getStawka().floatValue();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String s = sdf.format(new Date());
        Integer rok = Integer.valueOf(Integer.parseInt(s));
        //for(Iterator i$ = ((Spolka)l.get(nr)).getLWsi().iterator(); i$.hasNext();)
        for (Wies w : (ArrayList<Wies>) ((Spolka) l.get(nr)).getLWsi()) {
            //Wies w = (Wies)i$.next();
            //Iterator xx$ = w.getLKli().iterator();
            //while(i$.hasNext()) 
            for (Klient k : (ArrayList<Klient>) w.getLKli()) {
                //Klient k = (Klient)xx$.next();
                k.dodajNal(Float.valueOf(ile * k.getPow().floatValue()), rok);
            }
        }

    }

    public static Spolka getSpolka(int nr) {
        return ((Spolka) l.get(nr));
    }
    private static List l;
    private Date d;
    private String columnNames[] = {
        "Id", "Nazwa", "Stawka [zl]"
    };

    public void setProperNumbering(){
        ArrayList<Spolka> listaSpolek = (ArrayList<Spolka>) l;
        for(int i=0; i< listaSpolek.size(); i++){
            Spolka spolka = listaSpolek.get(i);
            spolka.id = i+1;
            ArrayList<Wies> listaWsi = (ArrayList<Wies>) spolka.getLWsi();
            for(int j=0; j< listaWsi.size(); j++){
                Wies wies = listaWsi.get(j);
                wies.id = j+1;
                wies.idSp = i+1;
                ArrayList<Klient> listaKlientow = (ArrayList<Klient>)wies.getLKli();
                for(int z=0; z<listaKlientow.size(); z++){
                    Klient klient = listaKlientow.get(z);
                    klient.nrKli = z+1;
                    klient.nrSp = i+1;
                    klient.nrWsi = j+1;
                }
            }
        }
    }
}
